package project.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class CurrentTaskPage extends Activity{
	
	int id;
	String name=null;
    String description=null;
	String deadline=null;
	int finished;
	String assignedby = null;
	String groupname = null;
	String assignedto = null;
	String taskGuid = null;
	
	
    public void onCreate(Bundle savedInstanceState) {
		
    	super.onCreate(savedInstanceState);
        //setContentView(R.layout.group_menu);
		
        //get guid of the current group
        Bundle extras = getIntent().getExtras(); 
        taskGuid = extras.getString("TASKGUID");
        String[] taskInfo = null;
        
        //connect to server to get the information of the current task
        try{
        	taskInfo = Synchronized.syncGroups(new DatabaseConnection(this),taskGuid,"tasks/gettask");
        }
        catch(Exception e){
        	new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getStackTrace().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   finish();
		           }
	        })
	        .show();
        }
        
        Log.w("deneme",taskInfo[0]);
        taskInfo = taskInfo[0].split("~");
        assignedto = taskInfo[0];
        name = taskInfo[1];
        description = taskInfo[2];
        deadline = taskInfo[3];
        assignedby = taskInfo[4];
        groupname = taskInfo[5];
        if(taskInfo[7] == "true")
        	finished = 1;
        else
        	finished = 0;
        
		
		//set view items
		setContentView(R.layout.task_page);
		EditText ed = (EditText)findViewById(R.id.namein);  
        ed.setText(name);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.descriptionin);  
        ed.setText(description);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.groupnamein);
        ed.setText(groupname);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.assignedbyin);
        ed.setText(assignedby);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.assignedtoin);
        ed.setText(assignedto);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.deadlinein);
        ed.setText(deadline.toString());
        ed.setEnabled(false);
        final CheckBox ch = (CheckBox)findViewById(R.id.finishedCheckBox);
        ch.setEnabled(false);
        if (finished == 1)
        	ch.setChecked(true);
        final CheckBox ch2 = (CheckBox)findViewById(R.id.overdateCheckBox);
        ch2.setEnabled(false);
        
        
    }

    public void editButtonOnClick(View view){
    	
    	//set view items
    			
    	EditText ed = (EditText)findViewById(R.id.namein);  
    	ed.setEnabled(true);
    	ed = (EditText)findViewById(R.id.descriptionin);
    	ed.setEnabled(true);
    	ed = (EditText)findViewById(R.id.deadlinein);
    	ed.setEnabled(true);
    	CheckBox ch = (CheckBox)findViewById(R.id.finishedCheckBox);
    	ch.setEnabled(true);
 	
    	
    }
    
    public void saveButtonOnClick(View view){
    	
    	name = ((EditText) findViewById(R.id.namein)).getText().toString();
    	description = ((EditText) findViewById(R.id.descriptionin)).getText().toString();
    	deadline = ((EditText) findViewById(R.id.deadlinein)).getText().toString();
    	String toBeSend = taskGuid+"*"+name+"*"+description+"*"+deadline+"*";
    	if(((CheckBox) findViewById(R.id.finishedCheckBox)).isChecked() == true)
    		toBeSend += "true";
    	else
    		toBeSend += "false";
    	
    	try{
    		Synchronized.syncGroups(new DatabaseConnection(this) , toBeSend , "tasks/updatetask");
    	}catch(Exception e){
    		new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getStackTrace().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   finish();
		           }
	        })
	        .show();
    	}
    	finish();
    }
    
    public void deleteButtonOnClick(View view){
    	
    	try{
    		Synchronized.syncGroups(new DatabaseConnection(this) , taskGuid , "tasks/deletetask");
    	}catch(Exception e){
    		new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getStackTrace().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   finish();
		           }
	        })
	        .show();
    	}
    	finish();
    	
    	
    }

}
